package com.suyang.syt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.WeixinMsgImageTextDao;
import com.suyang.syt.model.WeixinMsgImageText;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.WeixinMsgImageTextService;
import com.suyang.syt.util.EmptyUtil;

@Service
public class WeixinMsgImageTextServiceImpl implements WeixinMsgImageTextService {

	@Autowired
	private WeixinMsgImageTextDao weixinMsgImageTextDao;

	@Override
	public AjaxObj add(WeixinMsgImageText weixinMsgImageText, boolean isMaterial) throws Exception {
		if (weixinMsgImageText != null) {
			if (EmptyUtil.isEmpty(weixinMsgImageText.getTitle())) {
				return new AjaxObj(0, "图文标题不能为空");
			}
			if (isMaterial) {
				if (EmptyUtil.isEmpty(weixinMsgImageText.getThumb_media_id())) {
					return new AjaxObj(0, "封面素材不能为空");
				}
				if (EmptyUtil.isEmpty(weixinMsgImageText.getAuthor())) {
					return new AjaxObj(0, "图文作者不能为空");
				}
				if (EmptyUtil.isEmpty(weixinMsgImageText.getDigest())) {
					return new AjaxObj(0, "图文摘要不能为空");
				}
				if (EmptyUtil.isEmpty(weixinMsgImageText.getShow_cover_pic())) {
					return new AjaxObj(0, "是否显示封面不能为空");
				}
				if (EmptyUtil.isEmpty(weixinMsgImageText.getContent())) {
					return new AjaxObj(0, "图文信息内容不能为空");
				}
				if (EmptyUtil.isEmpty(weixinMsgImageText.getContent_source_url())) {
					return new AjaxObj(0, "图文消息的原文地址不能为空");
				}
			} else {
				if (EmptyUtil.isEmpty(weixinMsgImageText.getDescription())) {
					return new AjaxObj(0, "图文消息描述不能为空");
				}
				if (EmptyUtil.isEmpty(weixinMsgImageText.getPicUrl())) {
					return new AjaxObj(0, "图片链接不能为空");
				}
			}
			weixinMsgImageTextDao.add(weixinMsgImageText);
			return new AjaxObj(1, "素材添加成功!");
		}
		return new AjaxObj(0, "素材添加失败!");
	}

	@Override
	public AjaxObj deletes(String ids) throws Exception {
		if (EmptyUtil.isNotEmpty(ids)) {
			weixinMsgImageTextDao.deletes(ids);
			return new AjaxObj(1, "删除成功！");
		} else {
			return new AjaxObj(0, "请选择要删除的记录！");
		}
	}

	@Override
	public PagerEasyUI<WeixinMsgImageText> listByEasyUI(int page, int rows, String sort, String order, String createDate1, String createDate2, String isMaterial) throws Exception {
		StringBuilder hql = new StringBuilder(" from WeixinMsgImageText wm where 1=1 ");
		if (EmptyUtil.isNotEmpty(createDate1)) {
			hql.append(" and wm.createTime >= '").append(createDate1).append("' ");
		}
		if (EmptyUtil.isNotEmpty(createDate2)) {
			hql.append(" and wm.createTime <= '").append(createDate2).append("' ");
		}
		if (EmptyUtil.isNotEmpty(isMaterial)) {
			hql.append(" and wm.status=1 ");
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by wm.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<WeixinMsgImageText> easyUIPager = new PagerEasyUI<WeixinMsgImageText>();
		List<WeixinMsgImageText> list = weixinMsgImageTextDao.findByEasyUI(hql.toString(), page, rows);
		if (EmptyUtil.isNotEmpty(list)) {
			easyUIPager.setRows(list);
			easyUIPager.setTotal((Long) weixinMsgImageTextDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
			return easyUIPager;
		}
		return easyUIPager;
	}

}
